package model;

import java.util.ArrayList;
import java.util.Collections;

public class BalanceObjective {
	public int resourcea;
	public int resourceb;
	public int target;
	public int weightBalanceCost;
	
	public class BalancePair implements Comparable<BalancePair>{
		int id;
		long balance;
		
		public BalancePair(int id, long balance){
			this.id=id;
			this.balance = balance;
		}

		@Override
		public int compareTo(BalancePair o) {
			return (o.balance < balance)?-1:(o.balance > balance)?1:0;
		}

		@Override
		public String toString() {		
			return "["+id+","+ balance+"]";
		}

		public int getId() {
			return id;
		}

		public long getBalance() {
			return balance;
		}	
		
	}
	
	ArrayList<BalancePair> machineCapacityBalance;
	ArrayList<BalancePair> processRequirementsBalance;
	
	public void calcBalance(Problem problem){
//		System.out.println("BO "+toString());
		machineCapacityBalance = new ArrayList<BalanceObjective.BalancePair>(problem.M);
		for(int m=0;m<problem.M;m++){
			machineCapacityBalance.add(new BalancePair(m,problem.getC(m, resourcea)*target-problem.getC(m, resourceb)));
		}

//		System.out.println("MB:"+machineCapacityBalance);
		processRequirementsBalance = new ArrayList<BalanceObjective.BalancePair>(problem.P);
		for(int p=0;p<problem.P;p++){
			processRequirementsBalance.add(new BalancePair(p,problem.getR(p, resourcea)*target-problem.getR(p, resourceb)));
		}
		Collections.sort(processRequirementsBalance);
//		System.out.println("PB:"+processRequirementsBalance);
	}
	
	

	public String toString() {
		return String.format("%s-%s->%d weightBalanceCost=%d", resourcea, resourceb, target, weightBalanceCost);
	}



	public ArrayList<BalancePair> getProcessRequirementsBalance() {
		return processRequirementsBalance;
	}
}
